package com.example.gbuffer.RenderPass;

import android.content.Context;
import android.opengl.GLES32;

import com.example.gbuffer.Component.Camera.PerspectiveCamera;
import com.example.gbuffer.Component.Framebuffer.Framebuffer;
import com.example.gbuffer.Component.Light.PointLight;
import com.example.gbuffer.Component.Model.QuadModel;
import com.example.gbuffer.Component.Shader;

public class LightingRenderPass extends RenderPass{

    public static QuadModel m_quad = new QuadModel();
    private final static Shader m_quad_shader = new Shader();
    private static int m_position_texture_id = 0;
    private static int m_normal_texture_id = 0;
    private static int m_albedoSpce_texture_id = 0;

    private static PointLight[] m_point_light_list = new PointLight[]{new PointLight(), new PointLight()};
    public static PerspectiveCamera m_camera_p = new PerspectiveCamera();

    private final static String LOG_TAG = "LightingPass";

    public LightingRenderPass(Framebuffer buffer) {
        super(buffer);

        super.addDrawCall(LightingRenderPass::drawQuad);

        m_quad_shader.setName("LightingShader");
    }

    public static void setPositionTextureId(int textureId) {
        m_position_texture_id = textureId;
    }

    public static void setNormalTextureId(int textureId) {
        m_normal_texture_id = textureId;
    }

    public static void setAlbedoSpecTextureId(int textureId) {
        m_albedoSpce_texture_id = textureId;
    }

    public void setPerspectiveCamera(PerspectiveCamera camera) {
        m_camera_p = camera;
    }

    public void setPointLights(PointLight[] lights) {
        m_point_light_list = new PointLight[lights.length];
        System.arraycopy(lights, 0, m_point_light_list, 0, lights.length);
    }



    public void initResources(Context ctx) {

        // 设置光源位置
        m_point_light_list[0].setPosition(0.0f, 0.0f, 4.0f);
        m_point_light_list[1].setPosition(0.2f, 4.0f, -2.0f);

        // 加载模型数据
        m_quad.load();

        // 生成并编译着色器
        m_quad_shader.makeShaderProgramFromAssets(ctx, "lighting_pass.vs", "lighting_pass.fs");
    }


    private static Integer drawQuad() {

        GLES32.glClearColor(0.1f, 0.3f, 0.9f, 1.0f);
        GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT);
        GLES32.glDisable(GLES32.GL_DEPTH_TEST);

        GLES32.glActiveTexture(GLES32.GL_TEXTURE0);
        GLES32.glBindTexture(GLES32.GL_TEXTURE_2D, m_position_texture_id);
        GLES32.glActiveTexture(GLES32.GL_TEXTURE1);
        GLES32.glBindTexture(GLES32.GL_TEXTURE_2D, m_normal_texture_id);
        GLES32.glActiveTexture(GLES32.GL_TEXTURE2);
        GLES32.glBindTexture(GLES32.GL_TEXTURE_2D, m_albedoSpce_texture_id);

        m_quad_shader.use();
        m_quad_shader.setUniform1i("u_gPosition", 0);
        m_quad_shader.setUniform1i("u_gNormal", 1);
        m_quad_shader.setUniform1i("u_gAlbedoSpec", 2);

        m_quad_shader.setUniform3fv("u_viewPos", m_camera_p.getEye());

        for (int i = 0; i < m_point_light_list.length; i++) {
            m_quad_shader.setUniform3fv("u_lights[" + i + "].Position", m_point_light_list[i].getPosition());
            m_quad_shader.setUniform3fv("u_lights[" + i + "].Color", m_point_light_list[i].getLightColor());
        }

        m_quad.draw(m_quad_shader);

        return 0;
    }
}
